<?php

namespace app\api\controller;

use think\Controller;

class Goods extends Controller
{
    /**
     * 搜索
     */
    public function search($header, $data, $user)
    {
        $check = check_array($data, array('page', 'size', 'name'));
        if ($check['code'] != 1) {
            return $check;
        }
        $name = $data['name'];
        //以下是后台搜索词操作（可由app直接记录搜索词）
//        if ($header['uid'] > 0) {
//            $this->user_take_search($header['uid'], $name);
//        }
        //以上是后台搜索词操作

        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        if ($header['uid'] <= 0) {
            $uid = 0;
        } else {
            $uid = $header['uid'];
        }
        $goods = db('goods a')
            ->field("a.g_id,a.gl_ids,a.g_name,a.g_img,c.gb_name,(select c.uc_id from fr_user_collection c where c.u_id = $uid and c.g_id = a.g_id and uc_status = 1) as uc_id")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->join("fr_goods_brand c", "a.gb_id = c.gb_id", "left")
            ->where(array('a.g_status' => 1, 'b.gn_status' => 1, 'b.gn_stock' => array('gt', 0), 'a.g_name|c.gb_name' => array('like', '%' . $name . '%')))
            ->limit("$now,$size")
            ->order("a.g_sort desc")
            ->group("b.g_id")
            ->select();
        //产品列表（可以查询但，规格排序出错）
        if (!empty($goods)) {
            $ids = '0';
            foreach ($goods as $k => $v) {
                $ids .= ',' . $v['g_id'];
            }
            $norm = db('goods_norm a')
                ->field("a.g_id,a.gn_id,a.gn_name,a.gn_weight,a.gn_price,a.gn_one_number,a.gn_sort,a.gn_stock,a.gn_old_price,(select sum(c.g_number) from fr_goods_cart c where c.u_id = $uid and c.g_id = a.g_id and c.gn_id = a.gn_id and c.gc_status = 1) as g_number,a.gn_details_number,a.gn_details_unit")
                ->where(array('a.g_id' => array('in', $ids), 'a.gn_status' => 1, 'a.gn_stock' => array('gt', 0)))
                //显示多规格查询
//                ->where("a.g_id in (" . $ids . ") and a.gn_status = 1 and a.gn_stock > 0 and a.gn_sort = (select max(b.gn_sort) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0)")
                //单规格显示条件(主要是排序之后在分组查询会出问题)
//                ->group("a.g_id")
                //单规格显示需要
                ->order("a.gn_sort desc")
                ->select();
            foreach ($goods as $k => $v) {
                if ($v['uc_id'] > 0) {
                    $is_collection = 1;
                } else {
                    $is_collection = 0;
                }
                $goods1[$k] = array(
                    'g_id' => $v['g_id'],//商品id
                    'name' => $v['g_name'],//商品名称
                    'gb_name' => $v['gb_name'],//品牌名称
                    'img_url' => $v['g_img'],//图片地址
                    'is_collection' => $is_collection,//是否已收藏(1：已收藏)
                );
                if (preg_match('/,1,/', $v['gl_ids'])) {
                    //匹配成功
                    $goods1[$k]['special']=1;
                } else {
                    //匹配失败
                    $goods1[$k]['special']=0;
                }
                foreach ($norm as $kk => $vv) {
                    if ($vv['g_id'] == $v['g_id']) {
                        $weight_price = round($vv['gn_price'] / ($vv['gn_weight'] * 2), 2);
                        if ($vv['g_number'] <= 0) {
                            $vv['g_number'] = 0;
                        } else {
                            $vv['g_number'] = (int)$vv['g_number'];
                        }
                        $goods1[$k]['norm'] = array(
                            'gn_id' => $vv['gn_id'],//规格id
                            'name' => $vv['gn_name'],//规格名称
                            'weight' => $vv['gn_weight'],//总重量
                            'describe' => $vv['gn_one_number'],//单件描述
                            'price' => $vv['gn_price'],//售价
                            'old_price' => $vv['gn_old_price'],//原价
                            'weight_price' => $weight_price,//一斤价格
                            'stock' => $vv['gn_stock'],//库存
                            'g_number' => $vv['g_number'],//购物车数量
                            'detail_price' => round($vv['gn_price'] / ($vv['gn_details_number']), 2).'/'.$vv['gn_details_unit'],//详细价格
                        );
                        $goods2[]=$goods1[$k];
                        //多规格
                    }
                }
            }
//            $goods = $goods1;//单规格
            $goods = $goods2;//多规格
        }
        $count = db('goods a')
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->join("fr_goods_brand c", "a.gb_id = c.gb_id", "left")
            ->where(array('a.g_status' => 1, 'b.gn_status' => 1, 'b.gn_stock' => array('gt', 0), 'a.g_name|c.gb_name' => array('like', '%' . $name . '%')))
            ->group("b.g_id")
            ->count();
        $max_page=ceil($count/$size);
        if($max_page>0){
            return array('code' => 1, 'msg' => '搜索商品', 'data' => array('data' => $goods,'max_page'=>$max_page));
        }else{
            return array('code' => 2, 'msg' => '当前没有你想要的商品，请重新搜索.', 'data' => array());
        }
    }

    /**
     * 数据库对搜索词操作
     */
    function user_take_search($u_id, $name)
    {
        $search = db('search')
            ->where(array('u_id' => $u_id))
            ->find();
        if (empty($search)) {
            db('search')->insertGetId(array('u_id' => $u_id, 's_str' => $name));
        } else {
            if ($search['s_str'] != '') {
                $search1 = explode('|', $search['s_str']);
            } else {
                $search1 = array();
            }
            if (in_array($name, $search1)) {
                $search2[] = $name;
                foreach ($search1 as $v) {
                    if ($v != $name) {
                        $search2[] = $v;
                    }
                }
            } else {
                $search2[] = $name;
                foreach ($search1 as $v) {
                    $search2[] = $v;
                }
            }
            $search_str = implode('|', $search2);
            db('search')
                ->where(array('u_id' => $u_id))
                ->update(array('s_str' => $search_str));
        }
    }

    /**
     * 首页推荐产品
     */
    function recommend($header, $data, $user)
    {
        $check = check_array($data, array('page', 'size'));
        if ($check['code'] != 1) {
            return $check;
        }
        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        if ($header['uid'] <= 0) {
            $uid = 0;
        } else {
            $uid = $header['uid'];
        }
        $goods = db('goods a')
            ->field("a.g_id,a.gl_ids,a.g_name,a.g_img,(select c.uc_id from fr_user_collection c where c.u_id = $uid and c.g_id = a.g_id and uc_status = 1) as uc_id")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where(array('a.g_status' => 1, 'b.gn_status' => 1, 'b.gn_stock' => array('gt', 0),'a.g_is_index_recommen'=>1))
            ->limit("$now,$size")
            ->order("a.g_is_index_recommen,a.g_id desc")
            ->group("b.g_id")
            ->select();
        //产品列表（可以查询但，规格排序出错）
        if (!empty($goods)) {
            $ids = '0';
            foreach ($goods as $k => $v) {
                $ids .= ',' . $v['g_id'];
            }
            $norm = db('goods_norm a')
                ->field("a.g_id,a.gn_id,a.gn_name,a.gn_weight,a.gn_price,a.gn_one_number,a.gn_sort,a.gn_stock,a.gn_old_price,(select sum(c.g_number) from fr_goods_cart c where c.u_id = $uid and c.g_id = a.g_id and c.gn_id = a.gn_id and c.gc_status = 1) as g_number,a.gn_details_number,a.gn_details_unit")
                ->where(array('a.g_id'=>array('in',$ids),'a.gn_status'=>1,'a.gn_stock'=>array('gt',0)))
                //显示多规格查询
//                ->where("a.g_id in (" . $ids . ") and a.gn_status = 1 and a.gn_stock > 0 and a.gn_sort = (select max(b.gn_sort) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0)")
                //单规格显示条件(主要是排序之后在分组查询会出问题)
//                ->group("a.g_id")
                //单规格显示需要
                ->order("a.gn_sort desc")
                ->select();
            foreach ($goods as $k => $v) {
                if ($v['uc_id'] > 0) {
                    $is_collection = 1;
                } else {
                    $is_collection = 0;
                }
                $goods1[$k] = array(
                    'g_id' => $v['g_id'],//商品id
                    'name' => $v['g_name'],//商品名称
                    'img_url' => $v['g_img'],//图片地址
                    'is_collection' => $is_collection,//是否已收藏(1：已收藏)
                );
                if (preg_match('/,1,/', $v['gl_ids'])) {
                    //匹配成功
                    $goods1[$k]['special']=1;
                } else {
                    //匹配失败
                    $goods1[$k]['special']=0;
                }
                foreach ($norm as $kk => $vv) {
                    if ($vv['g_id'] == $v['g_id']) {
                        $weight_price = round($vv['gn_price'] / ($vv['gn_weight'] * 2), 2);
                        if ($vv['g_number'] <= 0) {
                            $vv['g_number'] = 0;
                        } else {
                            $vv['g_number'] = (int)$vv['g_number'];
                        }
                        $goods1[$k]['norm']= array(
                            'gn_id' => $vv['gn_id'],//规格id
                            'name' => $vv['gn_name'],//规格名称
                            'weight' => $vv['gn_weight'],//总重量
                            'describe' => $vv['gn_one_number'],//单件描述
                            'price' => $vv['gn_price'],//售价
                            'old_price' => $vv['gn_old_price'],//原价
                            'weight_price' => $weight_price,//一斤价格
                            'stock' => $vv['gn_stock'],//库存
                            'g_number' => $vv['g_number'],//库存
                            'detail_price' => round($vv['gn_price'] / ($vv['gn_details_number']), 2).'/'.$vv['gn_details_unit'],//详细价格
                        );
                        $goods2[]=$goods1[$k];
                        //多个数据
                    }
                }
            }
//            $goods = $goods1;//单规格
            $goods = $goods2;//多规格
        }
        $count=db('goods a')
            ->field("a.g_id,a.g_name,a.g_img,(select c.uc_id from fr_user_collection c where c.u_id = $uid and c.g_id = a.g_id and uc_status = 1) as uc_id")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where(array('a.g_status' => 1, 'b.gn_status' => 1, 'b.gn_stock' => array('gt', 0)))
            ->group("b.g_id")
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '推荐产品', 'data' => array('data' => $goods,'max_page'=>$max_page));
    }

    /**
     * 购物车操作
     */
    public function car_operation($header, $data, $user)
    {
        $check = check_array($data, array('g_id', 'gn_id', 'num'));
        if ($check['code'] != 1) {
            return $check;
        }
        $g_id = $data['g_id'];
        $gn_id = $data['gn_id'];
        $num = $data['num'];
        $goods = db('goods a')
            ->field("a.g_status,b.gn_status,b.gn_stock")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where(array('a.g_id' => $g_id, 'b.gn_id' => $gn_id))
            ->find();
        $data1 = array();
        if (empty($goods)) {
            return array('code' => 2, 'msg' => '商品出错,无法添加购物车', 'data' => $data1);
        }
        if ($goods['g_status'] != 1) {
            return array('code' => 2, 'msg' => '该商品已暂停出售', 'data' => $data1);
        }
        if ($goods['gn_status'] != 1) {
            return array('code' => 2, 'msg' => '该规格商品已暂停出售', 'data' => $data1);
        }
        if ($num > $goods['gn_stock']) {
            return array('code' => 2, 'msg' => '超出库存', 'data' => $data1);
        }
        $car = db('goods_cart')
            ->where(array('u_id' => $user['u_id'], 'g_id' => $g_id, 'gn_id' => $gn_id, 'gc_status' => 1))
            ->find();
        if (empty($car)) {
            if ($num == 0) {
                return array('code' => 2, 'msg' => '加入购物车的量必须大于0', 'data' => $data1);
            }
            $res = db('goods_cart')
                ->insertGetId(array('u_id' => $user['u_id'], 'g_id' => $g_id, 'gn_id' => $gn_id, 'gc_status' => 1, 'g_number' => $num, 'gc_create_time' => time()));
        } else {
            if ($num > 0) {
                $res = db('goods_cart')
                    ->where(array('u_id' => $user['u_id'], 'g_id' => $g_id, 'gn_id' => $gn_id, 'gc_status' => 1))
                    ->update(array('g_number' => $num));
            } else {
                $res = db('goods_cart')
                    ->where(array('u_id' => $user['u_id'], 'g_id' => $g_id, 'gn_id' => $gn_id, 'gc_status' => 1))
                    ->update(array('g_number' => 0, 'gc_end_time' => time(), 'gc_status' => 2));
            }
        }
        if ($res !== false) {
            return array('code' => 1, 'msg' => '购物车操作成功', 'data' => $data1);
        } else {
            return array('code' => 2, 'msg' => '购物车操作失败', 'data' => $data1);
        }
    }

    /**
     * 删除购物车商品
     */
    public function del_car_goods($header, $data, $user)
    {
        $check = check_array($data, array('ids'));
        if ($check['code'] != 1) {
            return $check;
        }
        $ids = $data['ids'];
        $res = db('goods_cart')
            ->where(array('u_id' => $user['u_id'], 'gc_status' => 1, 'gc_id' => array('in', $ids)))
            ->update(array('gc_end_time' => time(), 'gc_status' => 2));
        if ($res !== false) {
            return array('code' => 1, 'msg' => '删除购物车商品成功', 'data' => array());
        } else {
            return array('code' => 2, 'msg' => '删除购物车商品失败', 'data' => array());
        }
    }

    /**
     * 清空购物车
     */
    public function del_car($header, $data, $user)
    {
        $res = db('goods_cart')
            ->where(array('u_id' => $user['u_id'], 'gc_status' => 1))
            ->update(array('gc_end_time' => time(), 'gc_status' => 2));
        if ($res !== false) {
            return array('code' => 1, 'msg' => '清空购物车成功', 'data' => array());
        } else {
            return array('code' => 2, 'msg' => '清空购物车失败', 'data' => array());
        }
    }

    /**
     * 收藏
     */
    public function collect($header, $data, $user)
    {
        $check = check_array($data, array('g_id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $g_id = $data['g_id'];//产品id
        $collection = db('user_collection')
            ->where(array('u_id' => $user['u_id'], 'g_id' => $g_id, 'uc_status' => 1))
            ->find();
        if (!empty($collection)) {
            return array('code' => 2, 'msg' => '该商品已经收藏了', 'data' => array());
        }
        $res = db('user_collection')
            ->insertGetId(array('u_id' => $user['u_id'], 'g_id' => $g_id, 'uc_status' => 1, 'uc_create_time' => time()));
        if ($res > 0) {
            return array('code' => 1, 'msg' => '商品收藏成功', 'data' => array());
        } else {
            return array('code' => 2, 'msg' => '商品收藏失败', 'data' => array());
        }
    }

    /**
     * 取消收藏
     */
    public function del_collect($header, $data, $user)
    {
        $check = check_array($data, array('g_id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $g_id = $data['g_id'];//产品id
        $collection = db('user_collection')
            ->where(array('u_id' => $user['u_id'], 'g_id' => $g_id, 'uc_status' => 1))
            ->find();
        if (empty($collection)) {
            return array('code' => 2, 'msg' => '该商品还没有收藏', 'data' => array());
        }
        $res = db('user_collection')
            ->where(array('u_id' => $user['u_id'], 'g_id' => $g_id, 'uc_status' => 1))
            ->update(array('uc_status' => 2, 'uc_end_time' => time()));
        if ($res !== false) {
            return array('code' => 1, 'msg' => '取消收藏成功', 'data' => array());
        } else {
            return array('code' => 2, 'msg' => '取消收藏失败', 'data' => array());
        }
    }

    /**
     * 分类列表
     */
    public function type_list($header, $data, $user)
    {
        if (empty($data['id'])) {
            $id = 0;
        } else {
            $id = $data['id'];
            $type1 = db('goods_type')
                ->where(array('gt_id' => $id, 'gt_type' => 1, 'gt_status' => 1))
                ->find();
            if (empty($type1)) {
                $id = 0;
            }
        }
        if ($id == 0) {
            $type = db('goods_type')
                ->where(array('gt_type' => 1, 'gt_status' => 1))
                ->order("gt_sort desc")
                ->select();
            $pid = $type[0]['gt_id'];
        } else {
            $pid = $type1['gt_id'];
        }
        $type2 = db('goods_type')
            ->where(array('gt_pid' => $pid, 'gt_type' => 2, 'gt_status' => 1))
            ->order("gt_sort desc")
            ->select();
        if (!empty($type2)) {
            $ids = '0';
            foreach ($type2 as $k => $v) {
                $ids .= ',' . $v['gt_id'];
            }
            $type3 = db('goods_type')
                ->where(array('gt_pid' => array('in', $ids), 'gt_type' => 3, 'gt_status' => 1))
                ->order("gt_sort desc")
                ->select();
        } else {
            $type3 = array();
        }
        $children = array();
        foreach ($type2 as $k => $v) {
            $children[$k] = array('gt_id' => $v['gt_id'], 'name' => $v['gt_name'], 'img_url' => $v['gt_img']);
            if (!empty($type3)) {
                foreach ($type3 as $kk => $vv) {
                    if ($vv['gt_pid'] == $v['gt_id']) {
                        $children[$k]['child'][] = array('gt_id' => $vv['gt_id'], 'name' => $vv['gt_name'], 'img_url' => $vv['gt_img']);
                    }
                }
            }
        }
        $brand = db('goods_brand')
            ->where(array('gb_status' => 1, 'gb_type_ids' => array('like', '%,' . $pid . ',%')))
            ->order("gb_sort desc")
            ->select();
        if (!empty($brand)) {
            foreach ($brand as $v) {
                $brand1[] = array('gb_id' => $v['gb_id'], 'name' => $v['gb_name']);
            }
        } else {
            $brand1 = array();
        }
        if ($id == 0) {
            foreach ($type as $k => $v) {
                if ($k == 0) {
                    $default = 1;
                } else {
                    $default = 0;
                }
                $type0[] = array('gt_id' => $v['gt_id'], 'name' => $v['gt_name'], 'img_url' => $v['gt_img'], 'default' => $default);
            }
            $data1['type0'] = $type0;
        }
        $data1['type'] = $children;
        $data1['brand'] = $brand1;
        return array('code' => 1, 'msg' => '取消收藏成功', 'data' => $data1);
    }
    /**
     * 分类列表
     */
    public function all_type($header, $data, $user)
    {

        $type = db('goods_type')
            ->where(array( 'gt_status' => 1))
            ->order("gt_type,gt_sort desc")
            ->select();
        $data1[]=array();
        $i=0;
        foreach($type as $v){
            if($v['gt_type']==1){
                $data1[$i]=array('gt_id' => $v['gt_id'], 'name' => $v['gt_name'], 'img_url' => $v['gt_img']);
                $n=0;
                foreach($type as $vv){
                    if($vv['gt_type']==2 && $vv['gt_pid']==$v['gt_id']){
                        $data1[$i]['children'][$n]=array('gt_id' => $vv['gt_id'], 'name' => $vv['gt_name'], 'img_url' => $vv['gt_img']);
                        $m=0;
                        foreach($type as $vvv){
                            if($vvv['gt_type']==3 && $vvv['gt_pid']==$vv['gt_id']){
                                $data1[$i]['children'][$n]['children'][$m]=array('gt_id' => $vvv['gt_id'], 'name' => $vvv['gt_name'], 'img_url' => $vvv['gt_img']);
                                $m++;
                            }
                        }
                        $n++;
                    }
                }
                $i++;
            }
        }
        return array('code' => 1, 'msg' => '取消收藏成功', 'data' => $data1);
    }

    /**
     * 限时抢购计划列表
     */
    public function rush_list($header, $data, $user)
    {
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d', time() - 24 * 3600);
        $tomorrow = date('Y-m-d', time() + 24 * 3600);
        $rush1 = db('rush')
            ->where(array('r_date' => $today, 'r_status' => 1))
            ->find();
        $rush0 = db('rush')
            ->where(array('r_date' => $yesterday, 'r_status' => 1))
            ->find();
        $rush2 = db('rush')
            ->where(array('r_date' => $tomorrow, 'r_status' => 1))
            ->find();
        if (empty($rush0)) {
            $data1['rush0'] = array('id' => 0, 'rush' => array());
        } else {
            $data1['rush0'] = array('id' => 0, 'rush' => array('date' => $rush0['r_date'], 'start_time' => date('H:i', $rush0['r_start_time']), 'is_work' => 0));
        }
        if (empty($rush1)) {
            $data1['rush1'] = array('id' => 1, 'rush' => array());
        } else {
            if ($rush1['r_start_time'] <= time() && $rush1['r_end_time'] > time()) {
                $is_work = 1;
            } else {
                $is_work = 0;
            }
            $data1['rush1'] = array('id' => 1, 'rush' => array('date' => $rush1['r_date'], 'start_time' => date('H:i', $rush1['r_start_time']), 'is_work' => $is_work));
        }
        if (empty($rush2)) {
            $data1['rush2'] = array('id' => 2, 'rush' => array());
        } else {
            $data1['rush2'] = array('id' => 2, 'rush' => array('date' => $rush2['r_date'], 'start_time' => date('H:i', $rush2['r_start_time']), 'is_work' => 0));
        }
        return array('code' => 1, 'msg' => '限时抢购计划列表', 'data' => $data1);
    }

    /**
     * 限时抢购商品列表
     */
    public function rush_goods($header, $data, $user)
    {
        $check = check_array($data, array('page', 'size', 'id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $id = $data['id'];
        //以上是后台搜索词操作

        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        if ($header['uid'] <= 0) {
            $uid = 0;
        } else {
            $uid = $header['uid'];
        }
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d', time() - 24 * 3600);
        $tomorrow = date('Y-m-d', time() + 24 * 3600);
        if ($id == 0) {
            //昨天
            $rush = db('rush')
                ->where(array('r_date' => $yesterday, 'r_status' => 1))
                ->find();
        } else if ($id == 2) {
            //明天
            $rush = db('rush')
                ->where(array('r_date' => $tomorrow, 'r_status' => 1))
                ->find();
        } else {
            //今天
            $rush = db('rush')
                ->where(array('r_date' => $today, 'r_status' => 1, 'r_start_time' => array('elt', time()), 'r_end_time' => array('gt', time())))
                ->find();
        }
        if (empty($rush)) {
            return array('code' => 1, 'msg' => '限时抢购', 'data' => array('goods' => array()));
        }
        $r_id = $rush['r_id'];
        $goods = db("rush_goods c")
            ->field("a.g_id,a.gl_ids,a.g_name,a.g_img,(select c.uc_id from fr_user_collection c where c.u_id = $uid and c.g_id = a.g_id and uc_status = 1) as uc_id,c.rg_sales,rg_all")
            ->join("fr_goods a", "c.g_id = a.g_id", "left")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where("a.g_status = 1 and b.gn_status = 1 and b.gn_stock > 0 and c.r_id = $r_id and c.rg_status =1 and c.rg_sales < c.rg_all")
            ->limit("$now,$size")
            ->order("a.g_sort desc")
            ->group("b.g_id")
            ->select();
        //产品列表（可以查询但，规格排序出错）
        if (!empty($goods)) {
            $ids = '0';
            foreach ($goods as $k => $v) {
                $ids .= ',' . $v['g_id'];
            }
            $norm = db('goods_norm a')
                ->field("a.g_id,a.gn_id,a.gn_name,a.gn_weight,a.gn_price,a.gn_one_number,a.gn_sort,a.gn_stock,a.gn_old_price,(select sum(c.g_number) from fr_goods_cart c where c.u_id = $uid and c.g_id = a.g_id and c.gn_id = a.gn_id and c.gc_status = 1) as g_number,a.gn_details_number,a.gn_details_unit")
//                ->where(array('a.g_id' => array('in', $ids), 'a.gn_status' => 1, 'a.gn_stock' => array('gt', 0)))
                //显示多规格查询
                ->where("a.g_id in (" . $ids . ") and a.gn_status = 1 and a.gn_stock > 0 and a.gn_sort = (select max(b.gn_sort) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0)")
                //单规格显示条件(主要是排序之后在分组查询会出问题)
                ->group("a.g_id")
                //单规格显示需要
                ->order("a.gn_sort desc")
                ->select();
            foreach ($goods as $k => $v) {
                if ($v['uc_id'] > 0) {
                    $is_collection = 1;
                } else {
                    $is_collection = 0;
                }
                $goods1[$k] = array(
                    'g_id' => $v['g_id'],//商品id
                    'name' => $v['g_name'],//商品名称
                    'img_url' => $v['g_img'],//图片地址
                    'is_collection' => $is_collection,//是否已收藏(1：已收藏)
                    'sales' => $v['rg_sales'],//销售量
                    'percentage' => round($v['rg_sales'] / $v['rg_all'], 2) * 100,//抢购比例（百分比）
                );
                if (preg_match('/,1,/', $v['gl_ids'])) {
                    //匹配成功
                    $goods1[$k]['special']=1;
                } else {
                    //匹配失败
                    $goods1[$k]['special']=0;
                }
                foreach ($norm as $kk => $vv) {
                    if ($vv['g_id'] == $v['g_id']) {
                        $weight_price = round($vv['gn_price'] / ($vv['gn_weight'] * 2), 2);
                        if ($vv['g_number'] <= 0) {
                            $vv['g_number'] = 0;
                        } else {
                            $vv['g_number'] = (int)$vv['g_number'];
                        }
                        $goods1[$k]['norm'] = array(
                            'gn_id' => $vv['gn_id'],//规格id
                            'name' => $vv['gn_name'],//规格名称
                            'weight' => $vv['gn_weight'],//总重量
                            'describe' => $vv['gn_one_number'],//单件描述
                            'price' => $vv['gn_price'],//售价
                            'old_price' => $vv['gn_old_price'],//原价
                            'weight_price' => $weight_price,//一斤价格
                            'stock' => $vv['gn_stock'],//库存
                            'g_number' => $vv['g_number'],//库存
                            'detail_price' => round($vv['gn_price'] / ($vv['gn_details_number']), 2).'/'.$vv['gn_details_unit'],//详细价格
                        );
                    }
                }
            }
            $goods = $goods1;
        }
        $count = db("rush_goods c")
            ->join("fr_goods a", "c.g_id = a.g_id", "left")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where("a.g_status = 1 and b.gn_status = 1 and b.gn_stock > 0 and c.r_id = $r_id and c.rg_status =1 and c.rg_sales < c.rg_all")
            ->group("b.g_id")
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '推荐产品', 'data' => array('data' => $goods,'max_page'=>$max_page));
    }

    /**
     * 特价商品
     */
    public function special_goods($header, $data, $user)
    {
        $check = check_array($data, array('page', 'size'));
        if ($check['code'] != 1) {
            return $check;
        }
        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        if ($header['uid'] <= 0) {
            $uid = 0;
        } else {
            $uid = $header['uid'];
        }
        $goods = db('goods a')
            ->field("a.g_id,a.gl_ids,a.g_name,a.g_img,(select c.uc_id from fr_user_collection c where c.u_id = $uid and c.g_id = a.g_id and uc_status = 1) as uc_id")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where(array('a.g_status' => 1, 'b.gn_status' => 1, 'b.gn_stock' => array('gt', 0), 'a.gl_ids' => array('like', '%,1,%')))
            ->limit("$now,$size")
            ->order("a.g_sort desc")
            ->group("b.g_id")
            ->select();
        //产品列表（可以查询但，规格排序出错）
        if (!empty($goods)) {
            $ids = '0';
            foreach ($goods as $k => $v) {
                $ids .= ',' . $v['g_id'];
            }
            $norm = db('goods_norm a')
                ->field("a.g_id,a.gn_id,a.gn_name,a.gn_weight,a.gn_price,a.gn_one_number,a.gn_sort,a.gn_stock,a.gn_old_price,(select sum(c.g_number) from fr_goods_cart c where c.u_id = $uid and c.g_id = a.g_id and c.gn_id = a.gn_id and c.gc_status = 1) as g_number,a.gn_details_number,a.gn_details_unit")
                ->where(array('a.g_id' => array('in', $ids), 'a.gn_status' => 1, 'a.gn_stock' => array('gt', 0)))
                //显示多规格查询
//                ->where("a.g_id in (" . $ids . ") and a.gn_status = 1 and a.gn_stock > 0 and a.gn_sort = (select max(b.gn_sort) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0)")
                //单规格显示条件(主要是排序之后在分组查询会出问题)
//                ->group("a.g_id")
                //单规格显示需要
                ->order("a.gn_sort desc")
                ->select();
            foreach ($goods as $k => $v) {
                if ($v['uc_id'] > 0) {
                    $is_collection = 1;
                } else {
                    $is_collection = 0;
                }
                $goods1[$k] = array(
                    'g_id' => $v['g_id'],//商品id
                    'name' => $v['g_name'],//商品名称
                    'img_url' => $v['g_img'],//图片地址
                    'is_collection' => $is_collection,//是否已收藏(1：已收藏)
                );
                if (preg_match('/,1,/', $v['gl_ids'])) {
                    //匹配成功
                    $goods1[$k]['special']=1;
                } else {
                    //匹配失败
                    $goods1[$k]['special']=0;
                }
                foreach ($norm as $kk => $vv) {
                    if ($vv['g_id'] == $v['g_id']) {
                        $weight_price = round($vv['gn_price'] / ($vv['gn_weight'] * 2), 2);
                        if ($vv['g_number'] <= 0) {
                            $vv['g_number'] = 0;
                        } else {
                            $vv['g_number'] = (int)$vv['g_number'];
                        }
                        $goods1[$k]['norm'] = array(
                            'gn_id' => $vv['gn_id'],//规格id
                            'name' => $vv['gn_name'],//规格名称
                            'weight' => $vv['gn_weight'],//总重量
                            'describe' => $vv['gn_one_number'],//单件描述
                            'price' => $vv['gn_price'],//售价
                            'old_price' => $vv['gn_old_price'],//原价
                            'weight_price' => $weight_price,//一斤价格
                            'stock' => $vv['gn_stock'],//库存
                            'g_number' => $vv['g_number'],//库存
                            'detail_price' => round($vv['gn_price'] / ($vv['gn_details_number']), 2).'/'.$vv['gn_details_unit'],//详细价格
                        );
                        $goods2[]=$goods1[$k];
                        //多规格
                    }
                }
            }
//            $goods = $goods1;//单规格
            $goods = $goods2;//多规格
        }
        $count = db("goods a")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where(array('a.g_status' => 1, 'b.gn_status' => 1, 'b.gn_stock' => array('gt', 0), 'a.gl_ids' => array('like', '%,1,%')))
            ->group("b.g_id")
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '推荐产品', 'data' => array('data' => $goods,'max_page'=>$max_page));
    }

    /**
     * 商品标签列表
     */
    public function goods_label($header, $data, $user)
    {
        $label = db("goods_label")
            ->where(array("gl_status" => 1, "gl_id" => array('neq', 1)))
            ->order("gl_sort desc")
            ->select();
        $label1 = array();
        foreach ($label as $v) {
            $label1[] = array('gl_id' => $v['gl_id'], 'name' => $v['gl_name']);
        }
        return array('code' => 1, 'msg' => '商品标签', 'data' =>$label1);
    }

    /**
     * 分类商品
     */
    public function type_goods($header, $data, $user)
    {
        $check = check_array($data, array('page', 'size', 'order'));
        if ($check['code'] != 1) {
            return $check;
        }
        $where = "a.g_status = 1 and b.gn_status = 1 and b.gn_stock >0 ";
        if (empty($data['gl_id'])) {
            //标签id
        } else {
            $where .= " and a.gl_ids like '%," . $data['gl_id'] . ",%'";
        }
        if (empty($data['gb_id'])) {
            //品牌
        } else {
            $where .= " and a.gb_id = " . $data['gb_id'];
        }
        if (empty($data['gt_id'])) {
            //分类id
        } else {
            $where .= " and (a.gt_id1 = " . $data['gt_id'] . " or a.gt_id2 = " . $data['gt_id'] . " or a.gt_id = " . $data['gt_id'] . ")";
        }
        //以下是排序
        if ($data['order'] == 1) {
            $order = "a.g_sales desc";
        } else if ($data['order'] == 2) {
            $order = "a.g_sales";
        } else if ($data['order'] == 3) {
            $where .= " and b.gn_price = (select max(b.gn_price) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0) ";
            //因为group分钟只查询最先的一条，所以只查询一条售价最高的
            $order = "b.gn_price desc";
        } else if ($data['order'] == 4) {
            $where .= " and b.gn_price = (select min(b.gn_price) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0) ";
            //因为group分钟只查询最先的一条，所以只查询一条售价最低的
            $order = "b.gn_price";
        } else {
            $order = "a.g_sort desc";
        }
        //以上是排序
        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        if ($header['uid'] <= 0) {
            $uid = 0;
        } else {
            $uid = $header['uid'];
        }
        $goods = db('goods a')
            ->field("a.g_id,a.gl_ids,a.g_name,a.g_img,(select c.uc_id from fr_user_collection c where c.u_id = $uid and c.g_id = a.g_id and uc_status = 1) as uc_id")
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where($where)
            ->limit("$now,$size")
            ->order($order)
            ->group("b.g_id")
            ->select();
        //产品列表（可以查询但，规格排序出错）
        if (!empty($goods)) {
            $ids = '0';
            foreach ($goods as $k => $v) {
                $ids .= ',' . $v['g_id'];
            }
            $norm = db('goods_norm a')
                ->field("a.g_id,a.gn_id,a.gn_name,a.gn_weight,a.gn_price,a.gn_one_number,a.gn_sort,a.gn_stock,a.gn_old_price,(select sum(c.g_number) from fr_goods_cart c where c.u_id = $uid and c.g_id = a.g_id and c.gn_id = a.gn_id and c.gc_status = 1) as g_number,a.gn_details_number,a.gn_details_unit")
                ->where(array('a.g_id' => array('in', $ids), 'a.gn_status' => 1, 'a.gn_stock' => array('gt', 0)))
                //显示多规格查询
//                ->where("a.g_id in (" . $ids . ") and a.gn_status = 1 and a.gn_stock > 0 and a.gn_sort = (select max(b.gn_sort) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0)")
                //单规格显示条件(主要是排序之后在分组查询会出问题)
//                ->group("a.g_id")
                //单规格显示需要
                ->order("a.gn_sort desc")
                ->select();
            foreach ($goods as $k => $v) {
                if ($v['uc_id'] > 0) {
                    $is_collection = 1;
                } else {
                    $is_collection = 0;
                }
                $goods1[$k] = array(
                    'g_id' => $v['g_id'],//商品id
                    'name' => $v['g_name'],//商品名称
                    'img_url' => $v['g_img'],//图片地址
                    'is_collection' => $is_collection,//是否已收藏(1：已收藏)
                );
                if (preg_match('/,1,/', $v['gl_ids'])) {
                    //匹配成功
                    $goods1[$k]['special']=1;
                } else {
                    //匹配失败
                    $goods1[$k]['special']=0;
                }
                foreach ($norm as $kk => $vv) {
                    if ($vv['g_id'] == $v['g_id']) {
                        $weight_price = round($vv['gn_price'] / ($vv['gn_weight'] * 2), 2);
                        if ($vv['g_number'] <= 0) {
                            $vv['g_number'] = 0;
                        } else {
                            $vv['g_number'] = (int)$vv['g_number'];
                        }
                        $goods1[$k]['norm'][] = array(
                            'gn_id' => $vv['gn_id'],//规格id
                            'name' => $vv['gn_name'],//规格名称
                            'weight' => $vv['gn_weight'],//总重量
                            'describe' => $vv['gn_one_number'],//单件描述
                            'price' => $vv['gn_price'],//售价
                            'old_price' => $vv['gn_old_price'],//原价
                            'weight_price' => $weight_price,//一斤价格
                            'stock' => $vv['gn_stock'],//库存
                            'g_number' => $vv['g_number'],//库存
                            'detail_price' => round($vv['gn_price'] / ($vv['gn_details_number']), 2).'/'.$vv['gn_details_unit'],//详细价格
                        );
                    }
                }
            }
            $goods = $goods1;
        }
        $count = db('goods a')
            ->join("fr_goods_norm b", "a.g_id = b.g_id", "left")
            ->where($where)
            ->group("b.g_id")
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '推荐产品', 'data' => array('data' => $goods,'max_page'=>$max_page));
    }

    /**
     * 收藏商品分类
     */
    public function collect_type($header, $data, $user)
    {
        $collect = db('user_collection a')
            ->field("c.*")
            ->join("fr_goods b", "a.g_id = b.g_id", "right")
            ->join("fr_goods_type c", "b.gt_id1 = c.gt_id", "right")
            ->group("c.gt_id")
            ->order("c.gt_sort desc")
            ->where(array("a.u_id" => $user['u_id'], "a.uc_status" => 1))
            ->select();
        $type = array();
        foreach ($collect as $v) {
            $type[] = array('gt_id' => $v['gt_id'], 'name' => $v['gt_name'], 'img_url' => $v['gt_img']);
        }
        return array('code' => 1, 'msg' => '收藏产品分类', 'data' => $type);
    }

    /**
     * 我的收藏商品
     */
    public function my_collect($header, $data, $user)
    {
        $check = check_array($data, array('page', 'size'));
        if ($check['code'] != 1) {
            return $check;
        }
        $where = "b.uc_status = 1 and b.u_id = " . $user['u_id'];

        if (empty($data['gt_id'])) {
            //分类id
        } else {
            $where .= " and (a.gt_id1 = " . $data['gt_id'] . " or a.gt_id2 = " . $data['gt_id'] . " or a.gt_id = " . $data['gt_id'] . ")";
        }
        //以上是排序
        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        if ($header['uid'] <= 0) {
            $uid = 0;
        } else {
            $uid = $header['uid'];
        }
        $goods = db('goods a')
            ->field("a.g_id,a.gl_ids,a.g_name,a.g_img")
            ->join("fr_user_collection b", "a.g_id = b.g_id", "right")
            ->where($where)
            ->limit("$now,$size")
            ->order("b.uc_id desc")
            ->group("b.g_id")
            ->select();
        //产品列表（可以查询但，规格排序出错）
        if (!empty($goods)) {
            $ids = '0';
            foreach ($goods as $k => $v) {
                $ids .= ',' . $v['g_id'];
            }
            $norm = db('goods_norm a')
                ->field("a.g_id,a.gn_id,a.gn_name,a.gn_weight,a.gn_price,a.gn_one_number,a.gn_sort,a.gn_stock,a.gn_old_price,(select sum(c.g_number) from fr_goods_cart c where c.u_id = $uid and c.g_id = a.g_id and c.gn_id = a.gn_id and c.gc_status = 1) as g_number,a.gn_details_number,a.gn_details_unit")
                ->where(array('a.g_id' => array('in', $ids), 'a.gn_status' => 1, 'a.gn_stock' => array('gt', 0)))
                //显示多规格查询
//                ->where("a.g_id in (" . $ids . ") and a.gn_status = 1 and a.gn_stock > 0 and a.gn_sort = (select max(b.gn_sort) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0)")
                //单规格显示条件(主要是排序之后在分组查询会出问题)
//                ->group("a.g_id")
                //单规格显示需要
                ->order("a.gn_sort desc")
                ->select();
            foreach ($goods as $k => $v) {
                $is_collection = 1;
                $goods1[$k] = array(
                    'g_id' => $v['g_id'],//商品id
                    'name' => $v['g_name'],//商品名称
                    'img_url' => $v['g_img'],//图片地址
                    'is_collection' => $is_collection,//是否已收藏(1：已收藏)
                );
                if (preg_match('/,1,/', $v['gl_ids'])) {
                    //匹配成功
                    $goods1[$k]['special']=1;
                } else {
                    //匹配失败
                    $goods1[$k]['special']=0;
                }
                foreach ($norm as $kk => $vv) {
                    if ($vv['g_id'] == $v['g_id']) {
                        $weight_price = round($vv['gn_price'] / ($vv['gn_weight'] * 2), 2);
                        if ($vv['g_number'] <= 0) {
                            $vv['g_number'] = 0;
                        } else {
                            $vv['g_number'] = (int)$vv['g_number'];
                        }
                        $goods1[$k]['norm'][] = array(
                            'gn_id' => $vv['gn_id'],//规格id
                            'name' => $vv['gn_name'],//规格名称
                            'weight' => $vv['gn_weight'],//总重量
                            'describe' => $vv['gn_one_number'],//单件描述
                            'price' => $vv['gn_price'],//售价
                            'old_price' => $vv['gn_old_price'],//原价
                            'weight_price' => $weight_price,//一斤价格
                            'stock' => $vv['gn_stock'],//库存
                            'g_number' => $vv['g_number'],//库存
                            'detail_price' => round($vv['gn_price'] / ($vv['gn_details_number']), 2).'/'.$vv['gn_details_unit'],//详细价格
                        );
                    }
                }
            }
            $goods = $goods1;
        }
        $count = db('goods a')
            ->join("fr_user_collection b", "a.g_id = b.g_id", "right")
            ->where($where)
            ->group("b.g_id")
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '我的收藏', 'data' => array('data' => $goods,'max_page'=>$max_page));
    }

    /**
     * 常购商品分类
     */
    public function daily_type($header, $data, $user)
    {
        $collect = db('order_goods a')
            ->field("c.*")
            ->join("fr_goods b", "a.g_id = b.g_id", "right")
            ->join("fr_goods_type c", "b.gt_id1 = c.gt_id", "right")
            ->join("fr_order d", "a.o_id = d.o_id", "left")
            ->group("c.gt_id")
            ->order("c.gt_sort desc")
            ->where(array("a.u_id" => $user['u_id'], 'd.o_status' => array('gt', 0)))
            ->select();
        $type = array();
        foreach ($collect as $v) {
            $type[] = array('gt_id' => $v['gt_id'], 'name' => $v['gt_name'], 'img_url' => $v['gt_img']);
        }
        return array('code' => 1, 'msg' => '收藏产品分类', 'data' => $type);
    }

    /**
     * 常购商品
     */
    public function daily_goods($header, $data, $user)
    {
        $check = check_array($data, array('page', 'size'));
        if ($check['code'] != 1) {
            return $check;
        }
        $where = "b.u_id = " . $user['u_id'] . " and a.g_status = 1 and c.o_status >= 0";

        if (empty($data['gt_id'])) {
            //分类id
        } else {
            $where .= " and (a.gt_id1 = " . $data['gt_id'] . " or a.gt_id2 = " . $data['gt_id'] . " or a.gt_id = " . $data['gt_id'] . ")";
        }
        //以上是排序
        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        if ($header['uid'] <= 0) {
            $uid = 0;
        } else {
            $uid = $header['uid'];
        }
        $where .= " and b.og_id = (select max(og_id) from fr_order_goods c where c.g_id = a.g_id and c.u_id = b.u_id)";
        $goods = db('goods a')
            ->field("a.g_id,a.gl_ids,a.g_name,a.g_img,(select c.uc_id from fr_user_collection c where c.u_id = $uid and c.g_id = a.g_id and uc_status = 1) as uc_id")
            ->join("fr_order_goods b", "a.g_id = b.g_id", "right")
            ->join("fr_order c", "b.o_id = c.o_id", "left")
            ->where($where)
            ->limit("$now,$size")
            ->order("b.og_id desc")
            ->group("b.g_id")
            ->select();
        //产品列表（可以查询但，规格排序出错）
        if (!empty($goods)) {
            $ids = '0';
            foreach ($goods as $k => $v) {
                $ids .= ',' . $v['g_id'];
            }
            $norm = db('goods_norm a')
                ->field("a.g_id,a.gn_id,a.gn_name,a.gn_weight,a.gn_price,a.gn_one_number,a.gn_sort,a.gn_stock,a.gn_old_price,(select sum(c.g_number) from fr_goods_cart c where c.u_id = $uid and c.g_id = a.g_id and c.gn_id = a.gn_id and c.gc_status = 1) as g_number,a.gn_details_number,a.gn_details_unit")
                ->where(array('a.g_id' => array('in', $ids), 'a.gn_status' => 1, 'a.gn_stock' => array('gt', 0)))
                //显示多规格查询
//                ->where("a.g_id in (" . $ids . ") and a.gn_status = 1 and a.gn_stock > 0 and a.gn_sort = (select max(b.gn_sort) from fr_goods_norm b where b.g_id = a.g_id and b.gn_status = 1 and b.gn_stock > 0)")
                //单规格显示条件(主要是排序之后在分组查询会出问题)
//                ->group("a.g_id")
                //单规格显示需要
                ->order("a.gn_sort desc")
                ->select();
            foreach ($goods as $k => $v) {
                if ($v['uc_id'] > 0) {
                    $is_collection = 1;
                } else {
                    $is_collection = 0;
                }
                $goods1[$k] = array(
                    'g_id' => $v['g_id'],//商品id
                    'name' => $v['g_name'],//商品名称
                    'img_url' => $v['g_img'],//图片地址
                    'is_collection' => $is_collection,//是否已收藏(1：已收藏)
                );
                if (preg_match('/,1,/', $v['gl_ids'])) {
                    //匹配成功
                    $goods1[$k]['special']=1;
                } else {
                    //匹配失败
                    $goods1[$k]['special']=0;
                }
                foreach ($norm as $kk => $vv) {
                    if ($vv['g_id'] == $v['g_id']) {
                        $weight_price = round($vv['gn_price'] / ($vv['gn_weight'] * 2), 2);
                        if ($vv['g_number'] <= 0) {
                            $vv['g_number'] = 0;
                        } else {
                            $vv['g_number'] = (int)$vv['g_number'];
                        }
                        $goods1[$k]['norm'][] = array(
                            'gn_id' => $vv['gn_id'],//规格id
                            'name' => $vv['gn_name'],//规格名称
                            'weight' => $vv['gn_weight'],//总重量
                            'describe' => $vv['gn_one_number'],//单件描述
                            'price' => $vv['gn_price'],//售价
                            'old_price' => $vv['gn_old_price'],//原价
                            'weight_price' => $weight_price,//一斤价格
                            'stock' => $vv['gn_stock'],//库存
                            'g_number' => $vv['g_number'],//库存
                            'detail_price' => round($vv['gn_price'] / ($vv['gn_details_number']), 2).'/'.$vv['gn_details_unit'],//详细价格
                        );
                    }
                }
            }
            $goods = $goods1;
        }
        $count = db('goods a')
            ->join("fr_order_goods b", "a.g_id = b.g_id", "right")
            ->join("fr_order c", "b.o_id = c.o_id", "left")
            ->where($where)
            ->group("b.g_id")
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '推荐产品', 'data' => array('data' => $goods,'max_page'=>$max_page));
    }

    /**
     * 商品详情
     */
    public function goods_details($header, $data, $user)
    {
        $check = check_array($data, array('g_id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $g_id = $data['g_id'];
        if ($header['uid'] <= 0) {
            $uid = 0;
        } else {
            $uid = $header['uid'];
        }
        $goods = db("goods a")
            ->field("a.*,b.gb_name,d.gt_name,(select c.uc_id from fr_user_collection c where c.u_id = $uid and c.g_id = a.g_id and uc_status = 1) as uc_id")
            ->join("fr_goods_brand b", "a.gb_id=b.gb_id", "left")
            ->join("fr_goods_type d", "a.gt_id=d.gt_id", "left")
            ->where(array('a.g_id' => $g_id, 'a.g_status' => 1))
            ->find();
        if (empty($goods)) {
            return array('code' => 2, 'msg' => '商品出错，请刷新重试', 'data' => array());
        }
        $img = db("goods_img")
            ->where(array('g_id' => $g_id, 'gi_status' => 1))
            ->order("gi_sort desc")
            ->select();
        if ($goods['uc_id'] > 0) {
            $is_collection = 1;
        } else {
            $is_collection = 0;
        }
        if($goods['g_content']==''){
            $goods['g_content']='';
        }
        $goods1 = array(
            'g_id' => $goods['g_id'],//商品id
            'name' => $goods['g_name'],//商品名称
            'gb_name' => $goods['gb_name'],//品牌名称（可能为空）
            'gt_name' => $goods['gt_name'],//分类名称
//            'origin_place' => $goods['g_origin_place'],//产地
//            'manufactor' => $goods['g_manufactor'],//生成厂家
//            'storage' => $goods['g_storage'],//存储方式
//            'expiry' => $goods['g_expiry'],//保质期
//            'remarks' => $goods['g_remarks'],//备注
            'content' => $goods['g_content'],//详情
            'is_collection' => $is_collection,//是否收藏
        );

        $goods1['img'] = array();
        foreach ($img as $v) {
            $goods1['img'][] = array('id' => $v['gi_id'], 'img_url' => $v['gi_img']);
        }
        $norm = db("goods_norm a")
            ->field("a.g_id,a.gn_id,a.gn_name,a.gn_weight,a.gn_price,a.gn_one_number,a.gn_sort,a.gn_stock,a.gn_old_price,(select sum(c.g_number) from fr_goods_cart c where c.u_id = $uid and c.g_id = a.g_id and c.gn_id = a.gn_id and c.gc_status = 1) as g_number,a.gn_details_number,a.gn_details_unit")
            ->where(array('a.g_id' => $g_id, 'a.gn_status' => 1))
            ->order("a.gn_sort desc")
            ->select();
        $goods1['norm'] = array();
        foreach ($norm as $vv) {
            $weight_price = round($vv['gn_price'] / ($vv['gn_weight'] * 2), 2);
            if ($vv['g_number'] <= 0) {
                $vv['g_number'] = 0;
            } else {
                $vv['g_number'] = (int)$vv['g_number'];
            }
            $goods1['norm'][] = array(
                'gn_id' => $vv['gn_id'],//规格id
                'name' => $vv['gn_name'],//规格名称
                'weight' => $vv['gn_weight'],//总重量
                'describe' => $vv['gn_one_number'],//单件描述
                'price' => $vv['gn_price'],//售价
                'old_price' => $vv['gn_old_price'],//原价
                'weight_price' => $weight_price,//一斤价格
                'stock' => $vv['gn_stock'],//库存
                'g_number' => $vv['g_number'],//库存
                'detail_price' => round($vv['gn_price'] / ($vv['gn_details_number']), 2).'/'.$vv['gn_details_unit'],//详细价格
            );
        }
        $data1 = $goods1;
        return array('code' => 1, 'msg' => '产品详情', 'data' => $data1);
    }

}